#include "dbinfoeditor.h"
#include "ui_dbinfoeditor.h"
#include <QDebug>
#include <QMessageBox>

DbInfoEditor::DbInfoEditor(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DbInfoEditor)
{
    ui->setupUi(this);

}

DbInfoEditor::~DbInfoEditor()
{
    delete ui;
}

QString DbInfoEditor::connectName()
{
    return m_dbInfo.connName;
}

QString DbInfoEditor::hostName()
{
    return m_dbInfo.server;
}

QString DbInfoEditor::userName()
{
    return m_dbInfo.user;
}

QString DbInfoEditor::password()
{
    return m_dbInfo.passwd;
}

int DbInfoEditor::port()
{
    return m_dbInfo.port;
}

DatabaseInfo DbInfoEditor::databaseInfo()
{
    return m_dbInfo;
}

void DbInfoEditor::on_btnTestConn_clicked()
{
    MysqlDB db("test");
    db.setDatabaseInfo(m_dbInfo);
    if(!db.testConnect())
    {
        QMessageBox::information(this, Q_NULLPTR, "测试连接失败。");
    }
    else
    {
        QMessageBox::information(this, Q_NULLPTR, "测试连接成功。");
    }
}

void DbInfoEditor::on_btnOK_clicked()
{
    m_dbInfo.connName   = ui->lineEditName->text();
    m_dbInfo.server     = ui->lineEditServer->text();
    m_dbInfo.port       = ui->lineEditPort->text().toInt();
    m_dbInfo.user       = ui->lineEditUser->text();
    m_dbInfo.passwd     = ui->lineEditPasswd->text();


    emit accepted();
    accept();
}

void DbInfoEditor::on_btnCancel_clicked()
{
    if(m_dbInfo.connName.isEmpty())
    {
        QMessageBox::information(this, Q_NULLPTR, "请输入数据库连接名");
        return;
    }
    emit rejected();
    reject();
}

void DbInfoEditor::on_lineEditName_editingFinished()
{
    m_dbInfo.connName = ui->lineEditName->text();
}

void DbInfoEditor::on_lineEditServer_editingFinished()
{
    m_dbInfo.server = ui->lineEditServer->text();
}

void DbInfoEditor::on_lineEditPort_editingFinished()
{
    m_dbInfo.port = ui->lineEditPort->text().toInt();
}

void DbInfoEditor::on_lineEditUser_editingFinished()
{
    m_dbInfo.user = ui->lineEditUser->text();
}

void DbInfoEditor::on_lineEditPasswd_editingFinished()
{
    m_dbInfo.passwd = ui->lineEditPasswd->text();
}
